/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	fetp.h
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-11-26
**	Function List： 
**  History:         
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	防止重复包含
*********************************************************************************************************
*/

#ifndef _CAN_PROTOCOL_H_
#define _CAN_PROTOCOL_H_
/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/



/*
*********************************************************************************************************
*                                              	头文件
*********************************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
#define	CAN_DATA_SIZE_MAX		8

#define CAN_ID_MSK					0x07FF
#define CAN_ID_FN_MSK				0x0780
#define CAN_ID_NODE_MSK			0x007F

#define CAN_ID_GET_FN(id)				(id&CAN_ID_FN_MSK)
#define CAN_ID_GET_NODE(id)			(id&CAN_ID_NODE_MSK)
/*
*********************************************************************************************************
*                                              枚举和结构体类型定义
*********************************************************************************************************
*/
typedef uint16_t CAN_ID;
typedef enum{
	FN_SDO_T   = 0x600,
	FN_SDO_R   = 0x580,
	FN_NMT_TIM = 0x700,
	FN_NMT_SW  = 0x000,
	FN_SYNC    = 0x080,
	FN_TPDO1   = 0x180,
	FN_TPDO2   = 0x280,
	FN_EMGC    = 0x080,
}can_fn_t;

typedef enum{
	NMT_STATUS_ONLINE 	= 0x00,
	NMT_STATUS_STOP			= 0x04,
	NMT_STATUS_WORK			= 0x05,
	NMT_STATUS_PRE_WORK = 0x7F,
}nmt_status_t;

typedef enum{
	NMT_SW_ONLINE 	= 0x01,
	NMT_SW_STOP			= 0x02,
	NMT_SW_WORK			= 0x82,
}nmt_sw_t;

typedef enum{
	SDO_CS_WRITE			= 0x23,
	SDO_CS_WRITE_ACK 	= 0x60,
	SDO_CS_READ   		= 0x40,
	SDO_CS_READ_ACK 	= 0x43,
	SDO_CS_ERR				= 0x80,
}sdo_cs_t;

typedef enum{
	RESERVE_REG		= 0x00,
	FW_VERSION		= 0x01,
	HW_VERSION		= 0x02,
	DEVICE_TYPE		= 0x03,
	NMT_EN				= 0x21,
	NMT_INTERVAL	= 0x22,
	TPDO1					= 0x23,
	TPDO2					= 0x24,
}general_reg_t;

#pragma pack(1)
typedef struct{
	uint16_t	can_id;
	uint8_t		can_dlc;
	uint8_t		can_data[CAN_DATA_SIZE_MAX];
}can_msg_t;
#pragma pack()
/*
*********************************************************************************************************
*                                              	全局变量声明
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	函数声明
*********************************************************************************************************
*/
void can_read_reg(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg);
void can_write_reg(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg);
void can_nmt_sw(uint16_t can_id,nmt_status_t status,can_msg_t* p_msg);
void can_sync(can_msg_t* p_msg);
uint8_t is_sdo_cmd(can_msg_t* p_msg);

void can_read_reg_ack(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg);
void can_write_reg_ack(uint16_t can_id,uint8_t reg_addr,can_msg_t* p_msg);
void can_err_ack(uint16_t can_id,uint8_t err_code,can_msg_t* p_msg);
#endif
